// 调试工具类
export const debugUtil = {
  // 打印API请求信息
  logRequest(url, method, data) {
    console.group(`🚀 API Request: ${method.toUpperCase()} ${url}`)
    console.log('📤 Request Data:', data)
    console.log('⏰ Timestamp:', new Date().toISOString())
    console.groupEnd()
  },

  // 打印API响应信息
  logResponse(url, response, duration) {
    console.group(`✅ API Response: ${url}`)
    console.log('📥 Response Data:', response)
    console.log('⏱️ Duration:', `${duration}ms`)
    console.log('📊 Status:', response?.code || 'Unknown')
    console.log('💬 Message:', response?.msg || 'No message')
    console.groupEnd()
  },

  // 打印API错误信息
  logError(url, error, duration) {
    console.group(`❌ API Error: ${url}`)
    console.error('🚨 Error Details:', error)
    console.log('⏱️ Duration:', `${duration}ms`)
    console.log('🔍 Error Type:', error?.name || 'Unknown')
    console.log('📝 Error Message:', error?.message || 'No message')
    console.groupEnd()
  },

  // 测试后端连接
  async testConnection() {
    const startTime = Date.now()
    try {
      const response = await fetch('/api/user/findUserList', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({})
      })
      
      const duration = Date.now() - startTime
      
      if (response.ok) {
        const data = await response.json()
        this.logResponse('/api/user/findUserList', data, duration)
        return { success: true, data, duration }
      } else {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`)
      }
    } catch (error) {
      const duration = Date.now() - startTime
      this.logError('/api/user/findUserList', error, duration)
      return { success: false, error, duration }
    }
  }
}

// 全局调试开关
export const DEBUG_MODE = true

// 调试信息输出
if (DEBUG_MODE) {
  console.log('🔧 Debug Mode Enabled')
  console.log('📋 Available Debug Commands:')
  console.log('  - debugUtil.testConnection() - 测试后端连接')
  console.log('  - debugUtil.logRequest(url, method, data) - 记录请求')
  console.log('  - debugUtil.logResponse(url, response, duration) - 记录响应')
  console.log('  - debugUtil.logError(url, error, duration) - 记录错误')
}
